package com.mall507.fivepeaks.activity.find;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.Glide;
import com.facebook.drawee.generic.RootDrawable;
import com.hzh.frame.comn.annotation.ViewInject;
import com.hzh.frame.comn.callback.HttpCallBack;
import com.hzh.frame.core.HttpFrame.BaseHttp;
import com.hzh.frame.ui.activity.BaseUI;
import com.mall507.fivepeaks.R;
import com.mall507.fivepeaks.util.ImageUtil;
import com.mall507.fivepeaks.widget.imageview.ExpandImageView;

import org.json.JSONObject;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import static com.hzh.frame.util.ImageUtil.drawableToBitmap;
import static com.hzh.frame.util.ImageUtil.zoomBitmap;

@Route(path = "/find/FindCooperationUI")
public class FindCooperationUI extends BaseUI {
    @ViewInject(R.id.name)
    TextView name;
    @ViewInject(R.id.tel)
    TextView tel;
    @ViewInject(R.id.image)
    ExpandImageView image;
    

    private Dialog dialog;
    private String imgUri = "";
    private ImageView mImageView;
    
    @Override
    protected void onCreateBase() {
        setContentView(R.layout.ui_find_cooperation);
        getTitleView().setContent("设置");
        getChat();
    }
    
    private void getChat(){
        BaseHttp.getInstance().query("config/getContactInformation", new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject response) {
                if (response.optInt("code") == 200){
                    JSONObject data = response.optJSONObject("data");
                    name.setText("商务合作请电话联系" + data.optString("name"));
                    tel.setText("电话号码：" + data.optString("phone"));
                    imgUri = data.optString("qimg");
                    image.setImageURI(imgUri);
                }
                super.onSuccess(response);
            }
        });
        
        image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                init();
            }
        });
    }
    
    private void init() {
        //大图所依附的dialog
        dialog = new Dialog(FindCooperationUI.this, R.style.chooseDialog);
        LayoutInflater in = LayoutInflater.from(FindCooperationUI.this);
        View viewDialog = in.inflate(R.layout.view_xdialog_img, null);
        mImageView = viewDialog.findViewById(R.id.image);
        Glide.with(FindCooperationUI.this)
                .load(imgUri)
                .into(mImageView);
        dialog.setContentView(viewDialog);
        dialog.show();

        //大图的点击事件（点击让他消失）
        mImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        //大图的长按监听
        mImageView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                //弹出的“保存图片”的Dialog
                AlertDialog.Builder builder = new AlertDialog.Builder(FindCooperationUI.this);
                builder.setItems(new String[]{"保存"}, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        saveCroppedImage(((BitmapDrawable) mImageView.getDrawable()).getBitmap());
                    }
                });
                builder.show();
                return true;
            }
        });
    }

    //保存图片
    private void saveCroppedImage(Bitmap bmp) {
        File file = new File("/sdcard/fivePeaks/images");
        if (!file.exists())
            file.mkdir();
        
        String fileName = System.currentTimeMillis() + ".jpg";
        String newFilePath = "/sdcard/fivePeaks/images" + "/" + fileName;
        file = new File(newFilePath);
        try {
            file.createNewFile();
            FileOutputStream fos = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.JPEG, 50, fos);
            fos.flush();
            fos.close();
            Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            Uri uri = Uri.fromFile(file);
            intent.setData(uri);
            sendBroadcast(intent);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    
}
